package com.yang.aspect;

import com.yang.bean.MarketAdmin;
import com.yang.bean.MarketLog;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.yvo.ShipBO;
import com.yang.exception.WXException;
import com.yang.mapper.MarketLogMapper;
import com.yang.utils.IPAddressUtils;
import com.yang.utils.UserUtil;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.Period;
import java.util.Date;
import java.util.Map;

@Component
@Aspect
@Order(1)
public class CustomAspect {
    @Autowired
    DefaultWebSessionManager defaultWebSessionManager;
    @Autowired
    MarketLogMapper marketLogMapper;

    //登录的切口
    @Pointcut("execution(public * com.yang.controller.admin.AuthController.login(..))")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //TODO 方法执行的返回结果
        BaseRespVo proceed = (BaseRespVo) joinPoint.proceed();
        //TODO 获取login的username
        Object[] args = joinPoint.getArgs();
        Map arg = (Map) args[0];
        String username = (String) arg.get("username");
        String ipAdrress = null;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ipAdrress = IPAddressUtils.getIpAdrress(request);
        } catch (Exception e) {
            MarketLog marketLog = new MarketLog();
            marketLog.setAdmin(username);
            if (ipAdrress != null) {
                marketLog.setIp(ipAdrress);
            }
            marketLog.setType(1);
            marketLog.setAction("登录");
            marketLog.setAddTime(new Date());
            marketLog.setStatus(false);
            marketLogMapper.insertSelective(marketLog);

            throw new WXException("获取ip地址异常");
        }
        //TODO 执行的结果插入到数据库
        MarketLog marketLog = new MarketLog();
        marketLog.setAdmin(username);
        if (ipAdrress != null) {
            marketLog.setIp(ipAdrress);
        }
        marketLog.setType(1);
        marketLog.setAction("登录");
        marketLog.setAddTime(new Date());
        marketLog.setStatus(true);
        marketLogMapper.insertSelective(marketLog);
        return proceed;

    }

    @Pointcut("execution(public * com.yang.controller.admin.AuthController.logout(..))")
    public void servicePointcut1() {
    }

    @Around("servicePointcut1()")
    public Object around1(ProceedingJoinPoint joinPoint) throws Throwable {
        //TODO 方法执行的返回结果
        BaseRespVo proceed = (BaseRespVo) joinPoint.proceed();
        //TODO 获取login的username
        String ipAdrress = null;
        MarketAdmin adminMsg = null;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            adminMsg = UserUtil.getAdminMsg(request, defaultWebSessionManager);
            ipAdrress = IPAddressUtils.getIpAdrress(request);
        } catch (Exception e) {
            MarketLog marketLog = new MarketLog();
            if (adminMsg != null) {
                marketLog.setAdmin(adminMsg.getUsername());
            }
            if (ipAdrress != null) {
                marketLog.setIp(ipAdrress);
            }
            marketLog.setType(1);
            marketLog.setAction("登出");
            marketLog.setAddTime(new Date());
            marketLog.setStatus(false);
            marketLogMapper.insertSelective(marketLog);
            throw new WXException("获取ip地址异常");
        }
        //TODO 执行的结果插入到数据库
        MarketLog marketLog = new MarketLog();
        if (adminMsg != null) {
            marketLog.setAdmin(adminMsg.getUsername());
        }
        if (ipAdrress != null) {
            marketLog.setIp(ipAdrress);
        }
        marketLog.setType(1);
        marketLog.setAction("登出");
        marketLog.setAddTime(new Date());
        marketLog.setStatus(true);
        marketLogMapper.insertSelective(marketLog);
        return proceed;
    }

    //发货的切点
    @Pointcut("execution(public * com.yang.controller.admin.AdminOrderController.ship(..))")
    public void servicePointcut2() {
    }

    @Around("servicePointcut2()")
    public Object around2(ProceedingJoinPoint joinPoint) throws Throwable {
        //TODO 方法执行的返回结果
        BaseRespVo proceed = null;
        String ipAdrress = null;
        MarketAdmin adminMsg = null;
        String shipSn = null;
        try {
            proceed = (BaseRespVo) joinPoint.proceed();
            //商品的订单编号
            ShipBO data = (ShipBO) proceed.getData();
            shipSn = data.getShipSn();

            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            adminMsg = UserUtil.getAdminMsg(request, defaultWebSessionManager);
            ipAdrress = IPAddressUtils.getIpAdrress(request);
        } catch (Exception e) {
            MarketLog marketLog = new MarketLog();
            if (adminMsg != null) {
                marketLog.setAdmin(adminMsg.getUsername());
            }
            if (ipAdrress != null) {
                marketLog.setIp(ipAdrress);
            }
            marketLog.setType(2);
            marketLog.setAction("发货失败");
            marketLog.setAddTime(new Date());
            marketLog.setStatus(false);
            marketLogMapper.insertSelective(marketLog);
            throw new WXException("获取ip地址异常");
        }
        //TODO 执行的结果插入到数据库
        MarketLog marketLog = new MarketLog();
        if (adminMsg != null) {
            marketLog.setAdmin(adminMsg.getUsername());
        }
        if (ipAdrress != null) {
            marketLog.setIp(ipAdrress);
        }
        marketLog.setType(2);
        marketLog.setAction("发货成功");
        marketLog.setAddTime(new Date());
        marketLog.setStatus(true);
        marketLog.setResult("订单的编号： "+shipSn);
        marketLogMapper.insertSelective(marketLog);
        return proceed;
    }


}
